/**
 * @author planderos
 * usernamePopup.js
 * Handle popup
 */

function storeUserId()
{
	var dctk = connected.toolkit;
	var userIdEdit = dctk.getElementById("userIdEdit");
	
	var inputUserId = userIdEdit.text;
	
	var storage = connected.openStorage(netflixStorageUn,true);
	storage.set(netflixUserId,inputUserId);
	storePassword();
	loginState = 1;
}

function storePassword()
{
	var dctk = connected.toolkit;
	var passwordEdit = dctk.getElementById("passwordEdit");	
	
	var inputPassword = passwordEdit.text;
	
	var storage = connected.openStorage(netflixStoragePw,true);
	storage.set(netflixPassword,inputPassword);
	redirectLogin();
}

function popupButtonHandler(e)
{
	var dctk = connected.toolkit;

	switch(e.buttonId)
	{
		case dcKeyEvent.BUTTON_ID_BACK:
		{
			dctk.dismissPopup();
		}
		break;

	}
}

function showPopup()
{
	var dctk = connected.toolkit;
	
	var storageUn = connected.openStorage(netflixStorageUn,true);
	var username = storageUn.get(netflixUserId,username);
	
	var storagePw = connected.openStorage(netflixStoragePw,true);
	var password = storagePw.get(netflixPassword,password);
	
	// create a new panel for the popup
	var popup = dctk.createPanelCtrl("popup", true);
	popup.width = 240;
	popup.height = 160;
	popup.outlined = true;
	popup.filled = true;
	popup.color.rgb = 0x000000;
	popup.color.a = 192;
	popup.onUnhandledKeyEvent = popupButtonHandler;
	
	var queueHeadTxt = dctk.createLabelCtrl("queueHeadTxt", true);
	queueHeadTxt.setRect(0, 120, 240, 155);
	queueHeadTxt.textColor.rgb = 0xCDCDCD;
	queueHeadTxt.fontSize = 14;
	queueHeadTxt.text = "Netflix Account Info:\n(press ENTER for keyboard)";
	queueHeadTxt.align = dctkLabel.TS_CENTER;
	queueHeadTxt.valign = dctkLabel.TS_MIDDLE;
	popup.addChild(queueHeadTxt);
	
	var userIdTxt = dctk.createLabelCtrl("userIdTxt", true);
	userIdTxt.setRect(8, 85, 100, 115);
	userIdTxt.textColor.rgb = 0xCDCDCD;
	userIdTxt.fontSize = 16;
	userIdTxt.text = "User Name:";
	userIdTxt.align = dctkLabel.TS_RIGHT;
	userIdTxt.valign = dctkLabel.TS_MIDDLE;
	popup.addChild(userIdTxt);

	var userIdEdit = dctk.createEditCtrl("userIdEdit", true);
	userIdEdit.setRect(110, 90, 215, 110);
	userIdEdit.text = username;
	userIdEdit.fontSize = 16;
	userIdEdit.numeric = false;
	popup.addChild(userIdEdit);
	
	var passwordTxt = dctk.createLabelCtrl("passwordTxt", true);
	passwordTxt.setRect(8, 50, 100, 80);
	passwordTxt.textColor.rgb = 0xCDCDCD;
	passwordTxt.fontSize = 16;
	passwordTxt.text = "Password:";
	passwordTxt.align = dctkLabel.TS_RIGHT;
	passwordTxt.valign = dctkLabel.TS_MIDDLE;
	popup.addChild(passwordTxt);

	var passwordEdit = dctk.createEditCtrl("passwordEdit", true);
	passwordEdit.setRect(110, 55, 215, 75);
	passwordEdit.text = password;
	passwordEdit.fontSize = 16;
	passwordEdit.numeric = false;
	passwordEdit.password = true;
	popup.addChild(passwordEdit);

	var okBtn = dctk.createButtonCtrl("okBtn", true);
	okBtn.setRect(40, 10, 200, 40);
	okBtn.text = "Proceed to Login";
	okBtn.fontSize = 16;
	okBtn.onSelect = function(e) { storeUserId(); dctk.dismissPopup(); }
	popup.addChild(okBtn);
	
	// show the popup
	dctk.invokePopup(popup);
	
	// set the sibling mappings
	userIdEdit.topSibling = okBtn;
	userIdEdit.bottomSibling = passwordEdit;
	passwordEdit.topSibling = userIdEdit;
	passwordEdit.bottomSibling = okBtn;
	okBtn.topSibling = passwordEdit;
	okBtn.bottomSibling = userIdEdit;
		
	// start with the focus on the edit control
	dctk.focusedElement = userIdEdit;
}